
//  Created by 文 on 2019/12/19.
//  Copyright © 2019 GCY. All rights reserved.
//

#import "GeneralPopUp.h"

@interface GeneralPopUp ()

@property(strong, nonatomic)UIButton *codeImageBtn;

@property(strong, nonatomic)UILabel *mainTitleLabel;

@property(strong, nonatomic)UILabel *subTitleLabel;

@end

@implementation GeneralPopUp

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpUI];
    }
    
    return self;
}

- (void)setUpUI {
    
    _mainTitle = @"警告";
    _subTitle = @"确定要执行此操作吗？";
    _handleBtnTitleArray = @[@"取消", @"确定"];
    
    UIImageView *tipImage = [[UIImageView alloc] initWithFrame:CGRectMake((self.width-50)/2, 30, 50, 50)];
    tipImage.image = image(@"gcy_img_alert_lingdang");
    [self addSubview:tipImage];
    
    _mainTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, tipImage.cy_bottom + 10, self.width, 30)];
    _mainTitleLabel.text = self.mainTitle;
    _mainTitleLabel.textColor = [UIColor cy_colorWithHexString:@"#434C67"];
    _mainTitleLabel.font = font(18);
    _mainTitleLabel.textAlignment= NSTextAlignmentCenter;
    [self addSubview:_mainTitleLabel];
    
    _subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, _mainTitleLabel.cy_bottom, self.width-20, 40)];
    _subTitleLabel.text = self.subTitle;
    _subTitleLabel.textColor = [UIColor cy_colorWithHexString:@"#B6BED2"];
    _subTitleLabel.font = font(16);
    _subTitleLabel.numberOfLines = 0;
    _subTitleLabel.adjustsFontSizeToFitWidth = YES;
    _subTitleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_subTitleLabel];
    
    CGFloat btnWidth = (self.width-50)/2;
    
    for (int i=0; i<_handleBtnTitleArray.count; i++) {
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = i + 10;
        [button setTitle:_handleBtnTitleArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = font(16);
        [button setTitleColor:i==0?Theme_ColorValue:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setBackgroundColor:i==0?[UIColor whiteColor]:Theme_ColorValue];
        cut(button, BTN_BORDER_RADIUS);
        if (i==0) {
            cutBorder(button, 1, Theme_ColorValueString)
        }
        [button addTarget:self action:@selector(btnEventClick:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(20+(btnWidth+10)*i, self.height-70, btnWidth, 40);
        [self addSubview:button];
    }
    
}

- (void)btnEventClick:(UIButton *)button {
    
    if (button.tag == 10) {
        // 取消
        if (self.btnActionResultBlock) {
            self.btnActionResultBlock(NO, button.titleLabel.text, button.tag);
        }
    } else if (button.tag == 11) {
        // 确定
        if (self.btnActionResultBlock) {
            self.btnActionResultBlock(YES, button.titleLabel.text, button.tag);
        }
    }
}

- (void)setMainTitle:(NSString *)mainTitle {
    _mainTitle = mainTitle;
    _mainTitleLabel.text = _mainTitle;
}
- (void)setSubTitle:(NSString *)subTitle {
    _subTitle = subTitle;
    _subTitleLabel.text = _subTitle;
}
- (void)setHandleBtnTitleArray:(NSArray *)handleBtnTitleArray {
    _handleBtnTitleArray = handleBtnTitleArray;
    [_handleBtnTitleArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [self viewWithTag:10+idx];
        [btn setTitle:obj forState:UIControlStateNormal];
    }];
}


@end
